<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>inputhandler.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_ui_ac_inputhandler.js.html">inputhandler.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2006 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Class for managing the interactions between an
<a name="line17"></a> * auto-complete object and a text-input or textarea.
<a name="line18"></a> *
<a name="line19"></a> * IME note:
<a name="line20"></a> *
<a name="line21"></a> * We used to suspend autocomplete while there are IME preedit characters, but
<a name="line22"></a> * now for parity with Search we do not. We still detect the beginning and end
<a name="line23"></a> * of IME entry because we need to listen to more events while an IME commit is
<a name="line24"></a> * happening, but we update continuously as the user types.
<a name="line25"></a> *
<a name="line26"></a> * IMEs vary across operating systems, browsers, and even input languages. This
<a name="line27"></a> * class tries to handle IME for:
<a name="line28"></a> * - Windows x {FF3, IE7, Chrome} x MS IME 2002 (Japanese)
<a name="line29"></a> * - Mac     x {FF3, Safari3}     x Kotoeri (Japanese)
<a name="line30"></a> * - Linux   x {FF3}              x UIM + Anthy (Japanese)
<a name="line31"></a> *
<a name="line32"></a> * TODO(user): We cannot handle {Mac, Linux} x FF3 correctly.
<a name="line33"></a> * TODO(user): We need to support Windows x Google IME.
<a name="line34"></a> *
<a name="line35"></a> * This class was tested with hiragana input. The event sequence when inputting
<a name="line36"></a> * &#39;ai&lt;enter&gt;&#39; with IME on (which commits two characters) is as follows:
<a name="line37"></a> *
<a name="line38"></a> * Notation: [key down code, key press, key up code]
<a name="line39"></a> *           key code or +: event fired
<a name="line40"></a> *           -: event not fired
<a name="line41"></a> *
<a name="line42"></a> * - Win/FF3: [WIN_IME, +, A], [-, -, ENTER]
<a name="line43"></a> *            Note: No events are fired for &#39;i&#39;.
<a name="line44"></a> *
<a name="line45"></a> * - Win/IE7: [WIN_IME, -, A], [WIN_IME, -, I], [WIN_IME, -, ENTER]
<a name="line46"></a> *
<a name="line47"></a> * - Win/Chrome: Same as Win/IE7
<a name="line48"></a> *
<a name="line49"></a> * - Mac/FF3: [A, -, A], [I, -, I], [ENTER, -, ENTER]
<a name="line50"></a> *
<a name="line51"></a> * - Mac/Safari3: Same as Win/IE7
<a name="line52"></a> *
<a name="line53"></a> * - Linux/FF3: No events are generated.
<a name="line54"></a> *
<a name="line55"></a> * With IME off,
<a name="line56"></a> *
<a name="line57"></a> * - ALL: [A, +, A], [I, +, I], [ENTER, +, ENTER]
<a name="line58"></a> *        Note: Key code of key press event varies across configuration.
<a name="line59"></a> *
<a name="line60"></a> * With Microsoft Pinyin IME 3.0 (Simplified Chinese),
<a name="line61"></a> *
<a name="line62"></a> * - Win/IE7: Same as Win/IE7 with MS IME 2002 (Japanese)
<a name="line63"></a> *
<a name="line64"></a> *   The issue with this IME is that the key sequence that ends preedit is not
<a name="line65"></a> *   a single ENTER key up.
<a name="line66"></a> *   - ENTER key up following either ENTER or SPACE ends preedit.
<a name="line67"></a> *   - SPACE key up following even number of LEFT, RIGHT, or SPACE (any
<a name="line68"></a> *     combination) ends preedit.
<a name="line69"></a> *   TODO(user): We only support SPACE-then-ENTER sequence.
<a name="line70"></a> *   TODO(mpd): With the change to autocomplete during IME, this might not be an
<a name="line71"></a> *   issue. Remove this comment once tested.
<a name="line72"></a> *
<a name="line73"></a> * With Microsoft Korean IME 2002,
<a name="line74"></a> *
<a name="line75"></a> * - Win/IE7: Same as Win/IE7 with MS IME 2002 (Japanese), but there is no
<a name="line76"></a> *   sequence that ends the preedit.
<a name="line77"></a> *
<a name="line78"></a> * The following is the algorithm we use to detect IME preedit:
<a name="line79"></a> *
<a name="line80"></a> * - WIN_IME key down starts predit.
<a name="line81"></a> * - (1) ENTER key up or (2) CTRL-M key up ends preedit.
<a name="line82"></a> * - Any key press not immediately following WIN_IME key down signifies that
<a name="line83"></a> *   preedit has ended.
<a name="line84"></a> *
<a name="line85"></a> * If you need to change this algorithm, please note the OS, browser, language,
<a name="line86"></a> * and behavior above so that we can avoid regressions. Contact mpd or yuzo
<a name="line87"></a> * if you have questions or concerns.
<a name="line88"></a> *
<a name="line89"></a> */
<a name="line90"></a>
<a name="line91"></a>
<a name="line92"></a>goog.provide(&#39;goog.ui.ac.InputHandler&#39;);
<a name="line93"></a>
<a name="line94"></a>goog.require(&#39;goog.Disposable&#39;);
<a name="line95"></a>goog.require(&#39;goog.Timer&#39;);
<a name="line96"></a>goog.require(&#39;goog.a11y.aria&#39;);
<a name="line97"></a>goog.require(&#39;goog.asserts&#39;);
<a name="line98"></a>goog.require(&#39;goog.dom&#39;);
<a name="line99"></a>goog.require(&#39;goog.dom.selection&#39;);
<a name="line100"></a>goog.require(&#39;goog.events.EventHandler&#39;);
<a name="line101"></a>goog.require(&#39;goog.events.EventType&#39;);
<a name="line102"></a>goog.require(&#39;goog.events.KeyCodes&#39;);
<a name="line103"></a>goog.require(&#39;goog.events.KeyHandler&#39;);
<a name="line104"></a>goog.require(&#39;goog.events.KeyHandler.EventType&#39;);
<a name="line105"></a>goog.require(&#39;goog.string&#39;);
<a name="line106"></a>goog.require(&#39;goog.userAgent&#39;);
<a name="line107"></a>goog.require(&#39;goog.userAgent.product&#39;);
<a name="line108"></a>
<a name="line109"></a>
<a name="line110"></a>
<a name="line111"></a>/**
<a name="line112"></a> * Class for managing the interaction between an auto-complete object and a
<a name="line113"></a> * text-input or textarea.
<a name="line114"></a> *
<a name="line115"></a> * @param {?string=} opt_separators Separators to split multiple entries.
<a name="line116"></a> *     If none passed, uses &#39;,&#39; and &#39;;&#39;.
<a name="line117"></a> * @param {?string=} opt_literals Characters used to delimit text literals.
<a name="line118"></a> * @param {?boolean=} opt_multi Whether to allow multiple entries
<a name="line119"></a> *     (Default: true).
<a name="line120"></a> * @param {?number=} opt_throttleTime Number of milliseconds to throttle
<a name="line121"></a> *     keyevents with (Default: 150). Use -1 to disable updates on typing. Note
<a name="line122"></a> *     that typing the separator will update autocomplete suggestions.
<a name="line123"></a> * @constructor
<a name="line124"></a> * @extends {goog.Disposable}
<a name="line125"></a> */
<a name="line126"></a>goog.ui.ac.InputHandler = function(opt_separators, opt_literals,
<a name="line127"></a>    opt_multi, opt_throttleTime) {
<a name="line128"></a>  goog.Disposable.call(this);
<a name="line129"></a>  var throttleTime = opt_throttleTime || 150;
<a name="line130"></a>
<a name="line131"></a>  /**
<a name="line132"></a>   * Whether this input accepts multiple values
<a name="line133"></a>   * @type {boolean}
<a name="line134"></a>   * @private
<a name="line135"></a>   */
<a name="line136"></a>  this.multi_ = opt_multi != null ? opt_multi : true;
<a name="line137"></a>
<a name="line138"></a>  // Set separators depends on this.multi_ being set correctly
<a name="line139"></a>  this.setSeparators(opt_separators ||
<a name="line140"></a>      goog.ui.ac.InputHandler.STANDARD_LIST_SEPARATORS);
<a name="line141"></a>
<a name="line142"></a>  /**
<a name="line143"></a>   * Characters that are used to delimit literal text. Separarator characters
<a name="line144"></a>   * found within literal text are not processed as separators
<a name="line145"></a>   * @type {string}
<a name="line146"></a>   * @private
<a name="line147"></a>   */
<a name="line148"></a>  this.literals_ = opt_literals || &#39;&#39;;
<a name="line149"></a>
<a name="line150"></a>  /**
<a name="line151"></a>   * Whether to prevent the default behavior (moving focus to another element)
<a name="line152"></a>   * when tab is pressed.  This occurs by default only for multi-value mode.
<a name="line153"></a>   * @type {boolean}
<a name="line154"></a>   * @private
<a name="line155"></a>   */
<a name="line156"></a>  this.preventDefaultOnTab_ = this.multi_;
<a name="line157"></a>
<a name="line158"></a>  /**
<a name="line159"></a>   * A timer object used to monitor for changes when an element is active.
<a name="line160"></a>   *
<a name="line161"></a>   * TODO(user): Consider tuning the throttle time, so that it takes into
<a name="line162"></a>   * account the length of the token.  When the token is short it is likely to
<a name="line163"></a>   * match lots of rows, therefore we want to check less frequently.  Even
<a name="line164"></a>   * something as simple as &lt;3-chars = 150ms, then 100ms otherwise.
<a name="line165"></a>   *
<a name="line166"></a>   * @type {goog.Timer}
<a name="line167"></a>   * @private
<a name="line168"></a>   */
<a name="line169"></a>  this.timer_ = throttleTime &gt; 0 ? new goog.Timer(throttleTime) : null;
<a name="line170"></a>
<a name="line171"></a>  /**
<a name="line172"></a>   * Event handler used by the input handler to manage events.
<a name="line173"></a>   * @type {goog.events.EventHandler}
<a name="line174"></a>   * @private
<a name="line175"></a>   */
<a name="line176"></a>  this.eh_ = new goog.events.EventHandler(this);
<a name="line177"></a>
<a name="line178"></a>  /**
<a name="line179"></a>   * Event handler to help us find an input element that already has the focus.
<a name="line180"></a>   * @type {goog.events.EventHandler}
<a name="line181"></a>   * @private
<a name="line182"></a>   */
<a name="line183"></a>  this.activateHandler_ = new goog.events.EventHandler(this);
<a name="line184"></a>
<a name="line185"></a>  /**
<a name="line186"></a>   * The keyhandler used for listening on most key events.  This takes care of
<a name="line187"></a>   * abstracting away some of the browser differences.
<a name="line188"></a>   * @type {goog.events.KeyHandler}
<a name="line189"></a>   * @private
<a name="line190"></a>   */
<a name="line191"></a>  this.keyHandler_ = new goog.events.KeyHandler();
<a name="line192"></a>
<a name="line193"></a>  /**
<a name="line194"></a>   * The last key down key code.
<a name="line195"></a>   * @type {number}
<a name="line196"></a>   * @private
<a name="line197"></a>   */
<a name="line198"></a>  this.lastKeyCode_ = -1;  // Initialize to a non-existent value.
<a name="line199"></a>};
<a name="line200"></a>goog.inherits(goog.ui.ac.InputHandler, goog.Disposable);
<a name="line201"></a>
<a name="line202"></a>
<a name="line203"></a>/**
<a name="line204"></a> * Whether or not we need to pause the execution of the blur handler in order
<a name="line205"></a> * to allow the execution of the selection handler to run first. This is
<a name="line206"></a> * currently true when running on IOS version prior to 4.2, since we need
<a name="line207"></a> * some special logic for these devices to handle bug 4484488.
<a name="line208"></a> * @type {boolean}
<a name="line209"></a> * @private
<a name="line210"></a> */
<a name="line211"></a>goog.ui.ac.InputHandler.REQUIRES_ASYNC_BLUR_ =
<a name="line212"></a>    (goog.userAgent.product.IPHONE || goog.userAgent.product.IPAD) &amp;&amp;
<a name="line213"></a>        // Check the webkit version against the version for iOS 4.2.1.
<a name="line214"></a>        !goog.userAgent.isVersion(&#39;533.17.9&#39;);
<a name="line215"></a>
<a name="line216"></a>
<a name="line217"></a>/**
<a name="line218"></a> * Standard list separators.
<a name="line219"></a> * @type {string}
<a name="line220"></a> * @const
<a name="line221"></a> */
<a name="line222"></a>goog.ui.ac.InputHandler.STANDARD_LIST_SEPARATORS = &#39;,;&#39;;
<a name="line223"></a>
<a name="line224"></a>
<a name="line225"></a>/**
<a name="line226"></a> * Literals for quotes.
<a name="line227"></a> * @type {string}
<a name="line228"></a> * @const
<a name="line229"></a> */
<a name="line230"></a>goog.ui.ac.InputHandler.QUOTE_LITERALS = &#39;&quot;&#39;;
<a name="line231"></a>
<a name="line232"></a>
<a name="line233"></a>/**
<a name="line234"></a> * The AutoComplete instance this inputhandler is associated with.
<a name="line235"></a> * @type {goog.ui.ac.AutoComplete}
<a name="line236"></a> */
<a name="line237"></a>goog.ui.ac.InputHandler.prototype.ac_;
<a name="line238"></a>
<a name="line239"></a>
<a name="line240"></a>/**
<a name="line241"></a> * Characters that can be used to split multiple entries in an input string
<a name="line242"></a> * @type {string}
<a name="line243"></a> * @private
<a name="line244"></a> */
<a name="line245"></a>goog.ui.ac.InputHandler.prototype.separators_;
<a name="line246"></a>
<a name="line247"></a>
<a name="line248"></a>/**
<a name="line249"></a> * The separator we use to reconstruct the string
<a name="line250"></a> * @type {string}
<a name="line251"></a> * @private
<a name="line252"></a> */
<a name="line253"></a>goog.ui.ac.InputHandler.prototype.defaultSeparator_;
<a name="line254"></a>
<a name="line255"></a>
<a name="line256"></a>/**
<a name="line257"></a> * Regular expression used from trimming tokens or null for no trimming.
<a name="line258"></a> * @type {RegExp}
<a name="line259"></a> * @private
<a name="line260"></a> */
<a name="line261"></a>goog.ui.ac.InputHandler.prototype.trimmer_;
<a name="line262"></a>
<a name="line263"></a>
<a name="line264"></a>/**
<a name="line265"></a> * Regular expression to test whether a separator exists
<a name="line266"></a> * @type {RegExp}
<a name="line267"></a> * @private
<a name="line268"></a> */
<a name="line269"></a>goog.ui.ac.InputHandler.prototype.separatorCheck_;
<a name="line270"></a>
<a name="line271"></a>
<a name="line272"></a>/**
<a name="line273"></a> * Should auto-completed tokens be wrapped in whitespace?  Used in selectRow.
<a name="line274"></a> * @type {boolean}
<a name="line275"></a> * @private
<a name="line276"></a> */
<a name="line277"></a>goog.ui.ac.InputHandler.prototype.whitespaceWrapEntries_ = true;
<a name="line278"></a>
<a name="line279"></a>
<a name="line280"></a>/**
<a name="line281"></a> * Should the occurrence of a literal indicate a token boundary?
<a name="line282"></a> * @type {boolean}
<a name="line283"></a> * @private
<a name="line284"></a> */
<a name="line285"></a>goog.ui.ac.InputHandler.prototype.generateNewTokenOnLiteral_ = true;
<a name="line286"></a>
<a name="line287"></a>
<a name="line288"></a>/**
<a name="line289"></a> * Whether to flip the orientation of up &amp; down for hiliting next
<a name="line290"></a> * and previous autocomplete entries.
<a name="line291"></a> * @type {boolean}
<a name="line292"></a> * @private
<a name="line293"></a> */
<a name="line294"></a>goog.ui.ac.InputHandler.prototype.upsideDown_ = false;
<a name="line295"></a>
<a name="line296"></a>
<a name="line297"></a>/**
<a name="line298"></a> * If we&#39;re in &#39;multi&#39; mode, does typing a separator force the updating of
<a name="line299"></a> * suggestions?
<a name="line300"></a> * For example, if somebody finishes typing &quot;obama, hillary,&quot;, should the last
<a name="line301"></a> * comma trigger updating suggestions in a guaranteed manner? Especially useful
<a name="line302"></a> * when the suggestions depend on complete keywords. Note that &quot;obama, hill&quot;
<a name="line303"></a> * (a leading sub-string of &quot;obama, hillary&quot; will lead to different and possibly
<a name="line304"></a> * irrelevant suggestions.
<a name="line305"></a> * @type {boolean}
<a name="line306"></a> * @private
<a name="line307"></a> */
<a name="line308"></a>goog.ui.ac.InputHandler.prototype.separatorUpdates_ = true;
<a name="line309"></a>
<a name="line310"></a>
<a name="line311"></a>/**
<a name="line312"></a> * If we&#39;re in &#39;multi&#39; mode, does typing a separator force the current term to
<a name="line313"></a> * autocomplete?
<a name="line314"></a> * For example, if &#39;tomato&#39; is a suggested completion and the user has typed
<a name="line315"></a> * &#39;to,&#39;, do we autocomplete to turn that into &#39;tomato,&#39;?
<a name="line316"></a> * @type {boolean}
<a name="line317"></a> * @private
<a name="line318"></a> */
<a name="line319"></a>goog.ui.ac.InputHandler.prototype.separatorSelects_ = true;
<a name="line320"></a>
<a name="line321"></a>
<a name="line322"></a>/**
<a name="line323"></a> * The id of the currently active timeout, so it can be cleared if required.
<a name="line324"></a> * @type {?number}
<a name="line325"></a> * @private
<a name="line326"></a> */
<a name="line327"></a>goog.ui.ac.InputHandler.prototype.activeTimeoutId_ = null;
<a name="line328"></a>
<a name="line329"></a>
<a name="line330"></a>/**
<a name="line331"></a> * The element that is currently active.
<a name="line332"></a> * @type {Element}
<a name="line333"></a> * @private
<a name="line334"></a> */
<a name="line335"></a>goog.ui.ac.InputHandler.prototype.activeElement_ = null;
<a name="line336"></a>
<a name="line337"></a>
<a name="line338"></a>/**
<a name="line339"></a> * The previous value of the active element.
<a name="line340"></a> * @type {string}
<a name="line341"></a> * @private
<a name="line342"></a> */
<a name="line343"></a>goog.ui.ac.InputHandler.prototype.lastValue_ = &#39;&#39;;
<a name="line344"></a>
<a name="line345"></a>
<a name="line346"></a>/**
<a name="line347"></a> * Flag used to indicate that the IME key has been seen and we need to wait for
<a name="line348"></a> * the up event.
<a name="line349"></a> * @type {boolean}
<a name="line350"></a> * @private
<a name="line351"></a> */
<a name="line352"></a>goog.ui.ac.InputHandler.prototype.waitingForIme_ = false;
<a name="line353"></a>
<a name="line354"></a>
<a name="line355"></a>/**
<a name="line356"></a> * Flag used to indicate that the user just selected a row and we should
<a name="line357"></a> * therefore ignore the change of the input value.
<a name="line358"></a> * @type {boolean}
<a name="line359"></a> * @private
<a name="line360"></a> */
<a name="line361"></a>goog.ui.ac.InputHandler.prototype.rowJustSelected_ = false;
<a name="line362"></a>
<a name="line363"></a>
<a name="line364"></a>/**
<a name="line365"></a> * Flag indicating whether the result list should be updated continuously
<a name="line366"></a> * during typing or only after a short pause.
<a name="line367"></a> * @type {boolean}
<a name="line368"></a> * @private
<a name="line369"></a> */
<a name="line370"></a>goog.ui.ac.InputHandler.prototype.updateDuringTyping_ = true;
<a name="line371"></a>
<a name="line372"></a>
<a name="line373"></a>/**
<a name="line374"></a> * Attach an instance of an AutoComplete
<a name="line375"></a> * @param {goog.ui.ac.AutoComplete} ac Autocomplete object.
<a name="line376"></a> */
<a name="line377"></a>goog.ui.ac.InputHandler.prototype.attachAutoComplete = function(ac) {
<a name="line378"></a>  this.ac_ = ac;
<a name="line379"></a>};
<a name="line380"></a>
<a name="line381"></a>
<a name="line382"></a>/**
<a name="line383"></a> * Returns the associated autocomplete instance.
<a name="line384"></a> * @return {goog.ui.ac.AutoComplete} The associated autocomplete instance.
<a name="line385"></a> */
<a name="line386"></a>goog.ui.ac.InputHandler.prototype.getAutoComplete = function() {
<a name="line387"></a>  return this.ac_;
<a name="line388"></a>};
<a name="line389"></a>
<a name="line390"></a>
<a name="line391"></a>/**
<a name="line392"></a> * Returns the current active element.
<a name="line393"></a> * @return {Element} The currently active element.
<a name="line394"></a> */
<a name="line395"></a>goog.ui.ac.InputHandler.prototype.getActiveElement = function() {
<a name="line396"></a>  return this.activeElement_;
<a name="line397"></a>};
<a name="line398"></a>
<a name="line399"></a>
<a name="line400"></a>/**
<a name="line401"></a> * Returns the value of the current active element.
<a name="line402"></a> * @return {string} The value of the current active element.
<a name="line403"></a> */
<a name="line404"></a>goog.ui.ac.InputHandler.prototype.getValue = function() {
<a name="line405"></a>  return this.activeElement_.value;
<a name="line406"></a>};
<a name="line407"></a>
<a name="line408"></a>
<a name="line409"></a>/**
<a name="line410"></a> * Sets the value of the current active element.
<a name="line411"></a> * @param {string} value The new value.
<a name="line412"></a> */
<a name="line413"></a>goog.ui.ac.InputHandler.prototype.setValue = function(value) {
<a name="line414"></a>  this.activeElement_.value = value;
<a name="line415"></a>};
<a name="line416"></a>
<a name="line417"></a>
<a name="line418"></a>/**
<a name="line419"></a> * Returns the current cursor position.
<a name="line420"></a> * @return {number} The index of the cursor position.
<a name="line421"></a> */
<a name="line422"></a>goog.ui.ac.InputHandler.prototype.getCursorPosition = function() {
<a name="line423"></a>  return goog.dom.selection.getStart(this.activeElement_);
<a name="line424"></a>};
<a name="line425"></a>
<a name="line426"></a>
<a name="line427"></a>/**
<a name="line428"></a> * Sets the cursor at the given position.
<a name="line429"></a> * @param {number} pos The index of the cursor position.
<a name="line430"></a> */
<a name="line431"></a>goog.ui.ac.InputHandler.prototype.setCursorPosition = function(pos) {
<a name="line432"></a>  goog.dom.selection.setStart(this.activeElement_, pos);
<a name="line433"></a>  goog.dom.selection.setEnd(this.activeElement_, pos);
<a name="line434"></a>};
<a name="line435"></a>
<a name="line436"></a>
<a name="line437"></a>/**
<a name="line438"></a> * Attaches the input handler to a target element. The target element
<a name="line439"></a> * should be a textarea, input box, or other focusable element with the
<a name="line440"></a> * same interface.
<a name="line441"></a> * @param {Element|goog.events.EventTarget} target An element to attach the
<a name="line442"></a> *     input handler too.
<a name="line443"></a> */
<a name="line444"></a>goog.ui.ac.InputHandler.prototype.attachInput = function(target) {
<a name="line445"></a>  if (goog.dom.isElement(target)) {
<a name="line446"></a>    var el = /** @type {!Element} */ (target);
<a name="line447"></a>    goog.a11y.aria.setState(el, &#39;haspopup&#39;, true);
<a name="line448"></a>  }
<a name="line449"></a>
<a name="line450"></a>  this.eh_.listen(target, goog.events.EventType.FOCUS, this.handleFocus);
<a name="line451"></a>  this.eh_.listen(target, goog.events.EventType.BLUR, this.handleBlur);
<a name="line452"></a>
<a name="line453"></a>  if (!this.activeElement_) {
<a name="line454"></a>    this.activateHandler_.listen(
<a name="line455"></a>        target, goog.events.EventType.KEYDOWN,
<a name="line456"></a>        this.onKeyDownOnInactiveElement_);
<a name="line457"></a>
<a name="line458"></a>    // Don&#39;t wait for a focus event if the element already has focus.
<a name="line459"></a>    if (goog.dom.isElement(target)) {
<a name="line460"></a>      var ownerDocument = goog.dom.getOwnerDocument(
<a name="line461"></a>          /** @type {Element} */ (target));
<a name="line462"></a>      if (goog.dom.getActiveElement(ownerDocument) == target) {
<a name="line463"></a>        this.processFocus(/** @type {Element} */ (target));
<a name="line464"></a>      }
<a name="line465"></a>    }
<a name="line466"></a>  }
<a name="line467"></a>};
<a name="line468"></a>
<a name="line469"></a>
<a name="line470"></a>/**
<a name="line471"></a> * Detaches the input handler from the provided element.
<a name="line472"></a> * @param {Element|goog.events.EventTarget} target An element to detach the
<a name="line473"></a> *     input handler from.
<a name="line474"></a> */
<a name="line475"></a>goog.ui.ac.InputHandler.prototype.detachInput = function(target) {
<a name="line476"></a>  if (target == this.activeElement_) {
<a name="line477"></a>    this.handleBlur();
<a name="line478"></a>  }
<a name="line479"></a>  this.eh_.unlisten(target, goog.events.EventType.FOCUS, this.handleFocus);
<a name="line480"></a>  this.eh_.unlisten(target, goog.events.EventType.BLUR, this.handleBlur);
<a name="line481"></a>
<a name="line482"></a>  if (!this.activeElement_) {
<a name="line483"></a>    this.activateHandler_.unlisten(
<a name="line484"></a>        target, goog.events.EventType.KEYDOWN,
<a name="line485"></a>        this.onKeyDownOnInactiveElement_);
<a name="line486"></a>  }
<a name="line487"></a>};
<a name="line488"></a>
<a name="line489"></a>
<a name="line490"></a>/**
<a name="line491"></a> * Attaches the input handler to multiple elements.
<a name="line492"></a> * @param {...Element} var_args Elements to attach the input handler too.
<a name="line493"></a> */
<a name="line494"></a>goog.ui.ac.InputHandler.prototype.attachInputs = function(var_args) {
<a name="line495"></a>  for (var i = 0; i &lt; arguments.length; i++) {
<a name="line496"></a>    this.attachInput(arguments[i]);
<a name="line497"></a>  }
<a name="line498"></a>};
<a name="line499"></a>
<a name="line500"></a>
<a name="line501"></a>/**
<a name="line502"></a> * Detaches the input handler from multuple elements.
<a name="line503"></a> * @param {...Element} var_args Variable arguments for elements to unbind from.
<a name="line504"></a> */
<a name="line505"></a>goog.ui.ac.InputHandler.prototype.detachInputs = function(var_args) {
<a name="line506"></a>  for (var i = 0; i &lt; arguments.length; i++) {
<a name="line507"></a>    this.detachInput(arguments[i]);
<a name="line508"></a>  }
<a name="line509"></a>};
<a name="line510"></a>
<a name="line511"></a>
<a name="line512"></a>/**
<a name="line513"></a> * Selects the given row.  Implements the SelectionHandler interface.
<a name="line514"></a> * @param {Object} row The row to select.
<a name="line515"></a> * @param {boolean=} opt_multi Should this be treated as a single or multi-token
<a name="line516"></a> *     auto-complete?  Overrides previous setting of opt_multi on constructor.
<a name="line517"></a> * @return {boolean} Whether to suppress the update event.
<a name="line518"></a> */
<a name="line519"></a>goog.ui.ac.InputHandler.prototype.selectRow = function(row, opt_multi) {
<a name="line520"></a>  this.setTokenText(row.toString(), opt_multi);
<a name="line521"></a>  return false;
<a name="line522"></a>};
<a name="line523"></a>
<a name="line524"></a>
<a name="line525"></a>/**
<a name="line526"></a> * Sets the text of the current token without updating the autocomplete
<a name="line527"></a> * choices.
<a name="line528"></a> * @param {string} tokenText The text for the current token.
<a name="line529"></a> * @param {boolean=} opt_multi Should this be treated as a single or multi-token
<a name="line530"></a> *     auto-complete?  Overrides previous setting of opt_multi on constructor.
<a name="line531"></a> * @protected
<a name="line532"></a> */
<a name="line533"></a>goog.ui.ac.InputHandler.prototype.setTokenText =
<a name="line534"></a>    function(tokenText, opt_multi) {
<a name="line535"></a>  if (goog.isDef(opt_multi) ? opt_multi : this.multi_) {
<a name="line536"></a>    var index = this.getTokenIndex_(this.getValue(), this.getCursorPosition());
<a name="line537"></a>
<a name="line538"></a>    // Break up the current input string.
<a name="line539"></a>    var entries = this.splitInput_(this.getValue());
<a name="line540"></a>
<a name="line541"></a>    // Get the new value, ignoring whitespace associated with the entry.
<a name="line542"></a>    var replaceValue = tokenText;
<a name="line543"></a>
<a name="line544"></a>    // Only add punctuation if there isn&#39;t already a separator available.
<a name="line545"></a>    if (!this.separatorCheck_.test(replaceValue)) {
<a name="line546"></a>      replaceValue = goog.string.trimRight(replaceValue) +
<a name="line547"></a>                     this.defaultSeparator_;
<a name="line548"></a>    }
<a name="line549"></a>
<a name="line550"></a>    // Ensure there&#39;s whitespace wrapping the entries, if whitespaceWrapEntries_
<a name="line551"></a>    // has been set to true.
<a name="line552"></a>    if (this.whitespaceWrapEntries_) {
<a name="line553"></a>      if (index != 0 &amp;&amp; !goog.string.isEmpty(entries[index - 1])) {
<a name="line554"></a>        replaceValue = &#39; &#39; + replaceValue;
<a name="line555"></a>      }
<a name="line556"></a>      // Add a space only if it&#39;s the last token; otherwise, we assume the
<a name="line557"></a>      // next token already has the proper spacing.
<a name="line558"></a>      if (index == entries.length - 1) {
<a name="line559"></a>        replaceValue = replaceValue + &#39; &#39;;
<a name="line560"></a>      }
<a name="line561"></a>    }
<a name="line562"></a>
<a name="line563"></a>    // If the token needs changing, then update the input box and move the
<a name="line564"></a>    // cursor to the correct position.
<a name="line565"></a>    if (replaceValue != entries[index]) {
<a name="line566"></a>
<a name="line567"></a>      // Replace the value in the array.
<a name="line568"></a>      entries[index] = replaceValue;
<a name="line569"></a>
<a name="line570"></a>      var el = this.activeElement_;
<a name="line571"></a>      // If there is an uncommitted IME in Firefox or IE 9, setting the value
<a name="line572"></a>      // fails and results in actually clearing the value that&#39;s already in the
<a name="line573"></a>      // input.
<a name="line574"></a>      // The FF bug is http://bugzilla.mozilla.org/show_bug.cgi?id=549674
<a name="line575"></a>      // Blurring before setting the value works around this problem. We&#39;d like
<a name="line576"></a>      // to do this only if there is an uncommitted IME, but this isn&#39;t possible
<a name="line577"></a>      // to detect. Since text editing is finicky we restrict this
<a name="line578"></a>      // workaround to Firefox and IE 9 where it&#39;s necessary.
<a name="line579"></a>      if (goog.userAgent.GECKO ||
<a name="line580"></a>          (goog.userAgent.IE &amp;&amp; goog.userAgent.isVersion(&#39;9&#39;))) {
<a name="line581"></a>        el.blur();
<a name="line582"></a>      }
<a name="line583"></a>      // Join the array and replace the contents of the input.
<a name="line584"></a>      el.value = entries.join(&#39;&#39;);
<a name="line585"></a>
<a name="line586"></a>      // Calculate which position to put the cursor at.
<a name="line587"></a>      var pos = 0;
<a name="line588"></a>      for (var i = 0; i &lt;= index; i++) {
<a name="line589"></a>        pos += entries[i].length;
<a name="line590"></a>      }
<a name="line591"></a>
<a name="line592"></a>      // Set the cursor.
<a name="line593"></a>      el.focus();
<a name="line594"></a>      this.setCursorPosition(pos);
<a name="line595"></a>    }
<a name="line596"></a>  } else {
<a name="line597"></a>    this.setValue(tokenText);
<a name="line598"></a>  }
<a name="line599"></a>
<a name="line600"></a>  // Avoid triggering an autocomplete just because the value changed.
<a name="line601"></a>  this.rowJustSelected_ = true;
<a name="line602"></a>};
<a name="line603"></a>
<a name="line604"></a>
<a name="line605"></a>/** @override */
<a name="line606"></a>goog.ui.ac.InputHandler.prototype.disposeInternal = function() {
<a name="line607"></a>  goog.ui.ac.InputHandler.superClass_.disposeInternal.call(this);
<a name="line608"></a>  if (this.activeTimeoutId_ != null) {
<a name="line609"></a>    // Need to check against null explicitly because 0 is a valid value.
<a name="line610"></a>    window.clearTimeout(this.activeTimeoutId_);
<a name="line611"></a>  }
<a name="line612"></a>  this.eh_.dispose();
<a name="line613"></a>  delete this.eh_;
<a name="line614"></a>  this.activateHandler_.dispose();
<a name="line615"></a>  this.keyHandler_.dispose();
<a name="line616"></a>};
<a name="line617"></a>
<a name="line618"></a>
<a name="line619"></a>/**
<a name="line620"></a> * Sets the entry separator characters.
<a name="line621"></a> *
<a name="line622"></a> * @param {string} separators The separator characters to set.
<a name="line623"></a> */
<a name="line624"></a>goog.ui.ac.InputHandler.prototype.setSeparators = function(separators) {
<a name="line625"></a>  this.separators_ = separators;
<a name="line626"></a>  this.defaultSeparator_ = this.separators_.substring(0, 1);
<a name="line627"></a>
<a name="line628"></a>  var wspaceExp = this.multi_ ? &#39;[\\s&#39; + this.separators_ + &#39;]+&#39; : &#39;[\\s]+&#39;;
<a name="line629"></a>
<a name="line630"></a>  this.trimmer_ = new RegExp(&#39;^&#39; + wspaceExp + &#39;|&#39; + wspaceExp + &#39;$&#39;, &#39;g&#39;);
<a name="line631"></a>  this.separatorCheck_ = new RegExp(&#39;\\s*[&#39; + this.separators_ + &#39;]$&#39;);
<a name="line632"></a>};
<a name="line633"></a>
<a name="line634"></a>
<a name="line635"></a>/**
<a name="line636"></a> * Sets whether to flip the orientation of up &amp; down for hiliting next
<a name="line637"></a> * and previous autocomplete entries.
<a name="line638"></a> * @param {boolean} upsideDown Whether the orientation is upside down.
<a name="line639"></a> */
<a name="line640"></a>goog.ui.ac.InputHandler.prototype.setUpsideDown = function(upsideDown) {
<a name="line641"></a>  this.upsideDown_ = upsideDown;
<a name="line642"></a>};
<a name="line643"></a>
<a name="line644"></a>
<a name="line645"></a>/**
<a name="line646"></a> * Sets whether auto-completed tokens should be wrapped with whitespace.
<a name="line647"></a> * @param {boolean} newValue boolean value indicating whether or not
<a name="line648"></a> *     auto-completed tokens should be wrapped with whitespace.
<a name="line649"></a> */
<a name="line650"></a>goog.ui.ac.InputHandler.prototype.setWhitespaceWrapEntries =
<a name="line651"></a>    function(newValue) {
<a name="line652"></a>  this.whitespaceWrapEntries_ = newValue;
<a name="line653"></a>};
<a name="line654"></a>
<a name="line655"></a>
<a name="line656"></a>/**
<a name="line657"></a> * Sets whether new tokens should be generated from literals.  That is, should
<a name="line658"></a> * hello&#39;world be two tokens, assuming &#39; is a literal?
<a name="line659"></a> * @param {boolean} newValue boolean value indicating whether or not
<a name="line660"></a> * new tokens should be generated from literals.
<a name="line661"></a> */
<a name="line662"></a>goog.ui.ac.InputHandler.prototype.setGenerateNewTokenOnLiteral =
<a name="line663"></a>    function(newValue) {
<a name="line664"></a>  this.generateNewTokenOnLiteral_ = newValue;
<a name="line665"></a>};
<a name="line666"></a>
<a name="line667"></a>
<a name="line668"></a>/**
<a name="line669"></a> * Sets the regular expression used to trim the tokens before passing them to
<a name="line670"></a> * the matcher:  every substring that matches the given regular expression will
<a name="line671"></a> * be removed.  This can also be set to null to disable trimming.
<a name="line672"></a> * @param {RegExp} trimmer Regexp to use for trimming or null to disable it.
<a name="line673"></a> */
<a name="line674"></a>goog.ui.ac.InputHandler.prototype.setTrimmingRegExp = function(trimmer) {
<a name="line675"></a>  this.trimmer_ = trimmer;
<a name="line676"></a>};
<a name="line677"></a>
<a name="line678"></a>
<a name="line679"></a>/**
<a name="line680"></a> * Sets whether we will prevent the default input behavior (moving focus to the
<a name="line681"></a> * next focusable  element) on TAB.
<a name="line682"></a> * @param {boolean} newValue Whether to preventDefault on TAB.
<a name="line683"></a> */
<a name="line684"></a>goog.ui.ac.InputHandler.prototype.setPreventDefaultOnTab = function(newValue) {
<a name="line685"></a>  this.preventDefaultOnTab_ = newValue;
<a name="line686"></a>};
<a name="line687"></a>
<a name="line688"></a>
<a name="line689"></a>/**
<a name="line690"></a> * Sets whether separators perform autocomplete.
<a name="line691"></a> * @param {boolean} newValue Whether to autocomplete on separators.
<a name="line692"></a> */
<a name="line693"></a>goog.ui.ac.InputHandler.prototype.setSeparatorCompletes = function(newValue) {
<a name="line694"></a>  this.separatorUpdates_ = newValue;
<a name="line695"></a>  this.separatorSelects_ = newValue;
<a name="line696"></a>};
<a name="line697"></a>
<a name="line698"></a>
<a name="line699"></a>/**
<a name="line700"></a> * Sets whether separators perform autocomplete.
<a name="line701"></a> * @param {boolean} newValue Whether to autocomplete on separators.
<a name="line702"></a> */
<a name="line703"></a>goog.ui.ac.InputHandler.prototype.setSeparatorSelects = function(newValue) {
<a name="line704"></a>  this.separatorSelects_ = newValue;
<a name="line705"></a>};
<a name="line706"></a>
<a name="line707"></a>
<a name="line708"></a>/**
<a name="line709"></a> * Gets the time to wait before updating the results. If the update during
<a name="line710"></a> * typing flag is switched on, this delay counts from the last update,
<a name="line711"></a> * otherwise from the last keypress.
<a name="line712"></a> * @return {number} Throttle time in milliseconds.
<a name="line713"></a> */
<a name="line714"></a>goog.ui.ac.InputHandler.prototype.getThrottleTime = function() {
<a name="line715"></a>  return this.timer_ ? this.timer_.getInterval() : -1;
<a name="line716"></a>};
<a name="line717"></a>
<a name="line718"></a>
<a name="line719"></a>/**
<a name="line720"></a> * Sets whether a row has just been selected.
<a name="line721"></a> * @param {boolean} justSelected Whether or not the row has just been selected.
<a name="line722"></a> */
<a name="line723"></a>goog.ui.ac.InputHandler.prototype.setRowJustSelected = function(justSelected) {
<a name="line724"></a>  this.rowJustSelected_ = justSelected;
<a name="line725"></a>};
<a name="line726"></a>
<a name="line727"></a>
<a name="line728"></a>/**
<a name="line729"></a> * Sets the time to wait before updating the results.
<a name="line730"></a> * @param {number} time New throttle time in milliseconds.
<a name="line731"></a> */
<a name="line732"></a>goog.ui.ac.InputHandler.prototype.setThrottleTime = function(time) {
<a name="line733"></a>  if (time &lt; 0) {
<a name="line734"></a>    this.timer_.dispose();
<a name="line735"></a>    this.timer_ = null;
<a name="line736"></a>    return;
<a name="line737"></a>  }
<a name="line738"></a>  if (this.timer_) {
<a name="line739"></a>    this.timer_.setInterval(time);
<a name="line740"></a>  } else {
<a name="line741"></a>    this.timer_ = new goog.Timer(time);
<a name="line742"></a>  }
<a name="line743"></a>};
<a name="line744"></a>
<a name="line745"></a>
<a name="line746"></a>/**
<a name="line747"></a> * Gets whether the result list is updated during typing.
<a name="line748"></a> * @return {boolean} Value of the flag.
<a name="line749"></a> */
<a name="line750"></a>goog.ui.ac.InputHandler.prototype.getUpdateDuringTyping = function() {
<a name="line751"></a>  return this.updateDuringTyping_;
<a name="line752"></a>};
<a name="line753"></a>
<a name="line754"></a>
<a name="line755"></a>/**
<a name="line756"></a> * Sets whether the result list should be updated during typing.
<a name="line757"></a> * @param {boolean} value New value of the flag.
<a name="line758"></a> */
<a name="line759"></a>goog.ui.ac.InputHandler.prototype.setUpdateDuringTyping = function(value) {
<a name="line760"></a>  this.updateDuringTyping_ = value;
<a name="line761"></a>};
<a name="line762"></a>
<a name="line763"></a>
<a name="line764"></a>/**
<a name="line765"></a> * Handles a key event.
<a name="line766"></a> * @param {goog.events.BrowserEvent} e Browser event object.
<a name="line767"></a> * @return {boolean} True if the key event was handled.
<a name="line768"></a> * @protected
<a name="line769"></a> */
<a name="line770"></a>goog.ui.ac.InputHandler.prototype.handleKeyEvent = function(e) {
<a name="line771"></a>  switch (e.keyCode) {
<a name="line772"></a>
<a name="line773"></a>    // If the menu is open and &#39;down&#39; caused a change then prevent the default
<a name="line774"></a>    // action and prevent scrolling.  If the box isn&#39;t a multi autocomplete
<a name="line775"></a>    // and the menu isn&#39;t open, we force it open now.
<a name="line776"></a>    case goog.events.KeyCodes.DOWN:
<a name="line777"></a>      if (this.ac_.isOpen()) {
<a name="line778"></a>        this.moveDown_();
<a name="line779"></a>        e.preventDefault();
<a name="line780"></a>        return true;
<a name="line781"></a>
<a name="line782"></a>      } else if (!this.multi_) {
<a name="line783"></a>        this.update(true);
<a name="line784"></a>        e.preventDefault();
<a name="line785"></a>        return true;
<a name="line786"></a>      }
<a name="line787"></a>      break;
<a name="line788"></a>
<a name="line789"></a>    // If the menu is open and &#39;up&#39; caused a change then prevent the default
<a name="line790"></a>    // action and prevent scrolling.
<a name="line791"></a>    case goog.events.KeyCodes.UP:
<a name="line792"></a>      if (this.ac_.isOpen()) {
<a name="line793"></a>        this.moveUp_();
<a name="line794"></a>        e.preventDefault();
<a name="line795"></a>        return true;
<a name="line796"></a>      }
<a name="line797"></a>      break;
<a name="line798"></a>
<a name="line799"></a>    // If tab key is pressed, select the current highlighted item.  The default
<a name="line800"></a>    // action is also prevented if the input is a multi input, to prevent the
<a name="line801"></a>    // user tabbing out of the field.
<a name="line802"></a>    case goog.events.KeyCodes.TAB:
<a name="line803"></a>      if (this.ac_.isOpen() &amp;&amp; !e.shiftKey) {
<a name="line804"></a>        // Ensure the menu is up to date before completing.
<a name="line805"></a>        this.update();
<a name="line806"></a>        if (this.ac_.selectHilited() &amp;&amp; this.preventDefaultOnTab_) {
<a name="line807"></a>          e.preventDefault();
<a name="line808"></a>          return true;
<a name="line809"></a>        }
<a name="line810"></a>      } else {
<a name="line811"></a>        this.ac_.dismiss();
<a name="line812"></a>      }
<a name="line813"></a>      break;
<a name="line814"></a>
<a name="line815"></a>    // On enter, just select the highlighted row.
<a name="line816"></a>    case goog.events.KeyCodes.ENTER:
<a name="line817"></a>      if (this.ac_.isOpen()) {
<a name="line818"></a>        // Ensure the menu is up to date before completing.
<a name="line819"></a>        this.update();
<a name="line820"></a>        if (this.ac_.selectHilited()) {
<a name="line821"></a>          e.preventDefault();
<a name="line822"></a>          e.stopPropagation();
<a name="line823"></a>          return true;
<a name="line824"></a>        }
<a name="line825"></a>      } else {
<a name="line826"></a>        this.ac_.dismiss();
<a name="line827"></a>      }
<a name="line828"></a>      break;
<a name="line829"></a>
<a name="line830"></a>    // On escape tell the autocomplete to dismiss.
<a name="line831"></a>    case goog.events.KeyCodes.ESC:
<a name="line832"></a>      if (this.ac_.isOpen()) {
<a name="line833"></a>        this.ac_.dismiss();
<a name="line834"></a>        e.preventDefault();
<a name="line835"></a>        e.stopPropagation();
<a name="line836"></a>        return true;
<a name="line837"></a>      }
<a name="line838"></a>      break;
<a name="line839"></a>
<a name="line840"></a>    // The IME keycode indicates an IME sequence has started, we ignore all
<a name="line841"></a>    // changes until we get an enter key-up.
<a name="line842"></a>    case goog.events.KeyCodes.WIN_IME:
<a name="line843"></a>      if (!this.waitingForIme_) {
<a name="line844"></a>        this.startWaitingForIme_();
<a name="line845"></a>        return true;
<a name="line846"></a>      }
<a name="line847"></a>      break;
<a name="line848"></a>
<a name="line849"></a>    default:
<a name="line850"></a>      if (this.timer_ &amp;&amp; !this.updateDuringTyping_) {
<a name="line851"></a>        // Waits throttle time before sending the request again.
<a name="line852"></a>        this.timer_.stop();
<a name="line853"></a>        this.timer_.start();
<a name="line854"></a>      }
<a name="line855"></a>  }
<a name="line856"></a>
<a name="line857"></a>  return this.handleSeparator_(e);
<a name="line858"></a>};
<a name="line859"></a>
<a name="line860"></a>
<a name="line861"></a>/**
<a name="line862"></a> * Handles a key event for a separator key.
<a name="line863"></a> * @param {goog.events.BrowserEvent} e Browser event object.
<a name="line864"></a> * @return {boolean} True if the key event was handled.
<a name="line865"></a> * @private
<a name="line866"></a> */
<a name="line867"></a>goog.ui.ac.InputHandler.prototype.handleSeparator_ = function(e) {
<a name="line868"></a>  var isSeparatorKey = this.multi_ &amp;&amp; e.charCode &amp;&amp;
<a name="line869"></a>      this.separators_.indexOf(String.fromCharCode(e.charCode)) != -1;
<a name="line870"></a>  if (this.separatorUpdates_ &amp;&amp; isSeparatorKey) {
<a name="line871"></a>    this.update();
<a name="line872"></a>  }
<a name="line873"></a>  if (this.separatorSelects_ &amp;&amp; isSeparatorKey) {
<a name="line874"></a>    if (this.ac_.selectHilited()) {
<a name="line875"></a>      e.preventDefault();
<a name="line876"></a>      return true;
<a name="line877"></a>    }
<a name="line878"></a>  }
<a name="line879"></a>  return false;
<a name="line880"></a>};
<a name="line881"></a>
<a name="line882"></a>
<a name="line883"></a>/**
<a name="line884"></a> * @return {boolean} Whether this inputhandler need to listen on key-up.
<a name="line885"></a> * @protected
<a name="line886"></a> */
<a name="line887"></a>goog.ui.ac.InputHandler.prototype.needKeyUpListener = function() {
<a name="line888"></a>  return false;
<a name="line889"></a>};
<a name="line890"></a>
<a name="line891"></a>
<a name="line892"></a>/**
<a name="line893"></a> * Handles the key up event. Registered only if needKeyUpListener returns true.
<a name="line894"></a> * @param {goog.events.Event} e The keyup event.
<a name="line895"></a> * @return {boolean} Whether an action was taken or not.
<a name="line896"></a> * @protected
<a name="line897"></a> */
<a name="line898"></a>goog.ui.ac.InputHandler.prototype.handleKeyUp = function(e) {
<a name="line899"></a>  return false;
<a name="line900"></a>};
<a name="line901"></a>
<a name="line902"></a>
<a name="line903"></a>/**
<a name="line904"></a> * Adds the necessary input event handlers.
<a name="line905"></a> * @private
<a name="line906"></a> */
<a name="line907"></a>goog.ui.ac.InputHandler.prototype.addEventHandlers_ = function() {
<a name="line908"></a>  this.keyHandler_.attach(this.activeElement_);
<a name="line909"></a>  this.eh_.listen(
<a name="line910"></a>      this.keyHandler_, goog.events.KeyHandler.EventType.KEY, this.onKey_);
<a name="line911"></a>  if (this.needKeyUpListener()) {
<a name="line912"></a>    this.eh_.listen(this.activeElement_,
<a name="line913"></a>        goog.events.EventType.KEYUP, this.handleKeyUp);
<a name="line914"></a>  }
<a name="line915"></a>  this.eh_.listen(this.activeElement_,
<a name="line916"></a>      goog.events.EventType.MOUSEDOWN, this.onMouseDown_);
<a name="line917"></a>
<a name="line918"></a>  // IE also needs a keypress to check if the user typed a separator
<a name="line919"></a>  if (goog.userAgent.IE) {
<a name="line920"></a>    this.eh_.listen(this.activeElement_,
<a name="line921"></a>        goog.events.EventType.KEYPRESS, this.onIeKeyPress_);
<a name="line922"></a>  }
<a name="line923"></a>};
<a name="line924"></a>
<a name="line925"></a>
<a name="line926"></a>/**
<a name="line927"></a> * Removes the necessary input event handlers.
<a name="line928"></a> * @private
<a name="line929"></a> */
<a name="line930"></a>goog.ui.ac.InputHandler.prototype.removeEventHandlers_ = function() {
<a name="line931"></a>  this.eh_.unlisten(
<a name="line932"></a>      this.keyHandler_, goog.events.KeyHandler.EventType.KEY, this.onKey_);
<a name="line933"></a>  this.keyHandler_.detach();
<a name="line934"></a>  this.eh_.unlisten(this.activeElement_,
<a name="line935"></a>      goog.events.EventType.KEYUP, this.handleKeyUp);
<a name="line936"></a>  this.eh_.unlisten(this.activeElement_,
<a name="line937"></a>      goog.events.EventType.MOUSEDOWN, this.onMouseDown_);
<a name="line938"></a>
<a name="line939"></a>  if (goog.userAgent.IE) {
<a name="line940"></a>    this.eh_.unlisten(this.activeElement_,
<a name="line941"></a>        goog.events.EventType.KEYPRESS, this.onIeKeyPress_);
<a name="line942"></a>  }
<a name="line943"></a>
<a name="line944"></a>  if (this.waitingForIme_) {
<a name="line945"></a>    this.stopWaitingForIme_();
<a name="line946"></a>  }
<a name="line947"></a>};
<a name="line948"></a>
<a name="line949"></a>
<a name="line950"></a>/**
<a name="line951"></a> * Handles an element getting focus.
<a name="line952"></a> * @param {goog.events.Event} e Browser event object.
<a name="line953"></a> * @protected
<a name="line954"></a> */
<a name="line955"></a>goog.ui.ac.InputHandler.prototype.handleFocus = function(e) {
<a name="line956"></a>  this.processFocus(/** @type {Element} */ (e.target || null));
<a name="line957"></a>};
<a name="line958"></a>
<a name="line959"></a>
<a name="line960"></a>/**
<a name="line961"></a> * Registers handlers for the active element when it receives focus.
<a name="line962"></a> * @param {Element} target The element to focus.
<a name="line963"></a> * @protected
<a name="line964"></a> */
<a name="line965"></a>goog.ui.ac.InputHandler.prototype.processFocus = function(target) {
<a name="line966"></a>  this.activateHandler_.removeAll();
<a name="line967"></a>
<a name="line968"></a>  if (this.ac_) {
<a name="line969"></a>    this.ac_.cancelDelayedDismiss();
<a name="line970"></a>  }
<a name="line971"></a>
<a name="line972"></a>  // Double-check whether the active element has actually changed.
<a name="line973"></a>  // This is a fix for Safari 3, which fires spurious focus events.
<a name="line974"></a>  if (target != this.activeElement_) {
<a name="line975"></a>    this.activeElement_ = target;
<a name="line976"></a>    if (this.timer_) {
<a name="line977"></a>      this.timer_.start();
<a name="line978"></a>      this.eh_.listen(this.timer_, goog.Timer.TICK, this.onTick_);
<a name="line979"></a>    }
<a name="line980"></a>    this.lastValue_ = this.getValue();
<a name="line981"></a>    this.addEventHandlers_();
<a name="line982"></a>  }
<a name="line983"></a>};
<a name="line984"></a>
<a name="line985"></a>
<a name="line986"></a>/**
<a name="line987"></a> * Handles an element blurring.
<a name="line988"></a> * @param {goog.events.Event=} opt_e Browser event object.
<a name="line989"></a> * @protected
<a name="line990"></a> */
<a name="line991"></a>goog.ui.ac.InputHandler.prototype.handleBlur = function(opt_e) {
<a name="line992"></a>  // Phones running iOS prior to version 4.2.
<a name="line993"></a>  if (goog.ui.ac.InputHandler.REQUIRES_ASYNC_BLUR_) {
<a name="line994"></a>    // @bug 4484488 This is required so that the menu works correctly on
<a name="line995"></a>    // iOS prior to version 4.2. Otherwise, the blur action closes the menu
<a name="line996"></a>    // before the menu button click can be processed.
<a name="line997"></a>    // In order to fix the bug, we set a timeout to process the blur event, so
<a name="line998"></a>    // that any pending selection event can be processed first.
<a name="line999"></a>    this.activeTimeoutId_ =
<a name="line1000"></a>        window.setTimeout(goog.bind(this.processBlur_, this), 0);
<a name="line1001"></a>    return;
<a name="line1002"></a>  } else {
<a name="line1003"></a>    this.processBlur_();
<a name="line1004"></a>  }
<a name="line1005"></a>};
<a name="line1006"></a>
<a name="line1007"></a>
<a name="line1008"></a>/**
<a name="line1009"></a> * Helper function that does the logic to handle an element blurring.
<a name="line1010"></a> * @private
<a name="line1011"></a> */
<a name="line1012"></a>goog.ui.ac.InputHandler.prototype.processBlur_ = function() {
<a name="line1013"></a>  // it&#39;s possible that a blur event could fire when there&#39;s no active element,
<a name="line1014"></a>  // in the case where attachInput was called on an input that already had
<a name="line1015"></a>  // the focus
<a name="line1016"></a>  if (this.activeElement_) {
<a name="line1017"></a>    this.removeEventHandlers_();
<a name="line1018"></a>    this.activeElement_ = null;
<a name="line1019"></a>
<a name="line1020"></a>    if (this.timer_) {
<a name="line1021"></a>      this.timer_.stop();
<a name="line1022"></a>      this.eh_.unlisten(this.timer_, goog.Timer.TICK, this.onTick_);
<a name="line1023"></a>    }
<a name="line1024"></a>
<a name="line1025"></a>    if (this.ac_) {
<a name="line1026"></a>      // Pause dismissal slightly to take into account any other events that
<a name="line1027"></a>      // might fire on the renderer (e.g. a click will lose the focus).
<a name="line1028"></a>      this.ac_.dismissOnDelay();
<a name="line1029"></a>    }
<a name="line1030"></a>  }
<a name="line1031"></a>};
<a name="line1032"></a>
<a name="line1033"></a>
<a name="line1034"></a>/**
<a name="line1035"></a> * Handles the timer&#39;s tick event.  Calculates the current token, and reports
<a name="line1036"></a> * any update to the autocomplete.
<a name="line1037"></a> * @param {goog.events.Event} e Browser event object.
<a name="line1038"></a> * @private
<a name="line1039"></a> */
<a name="line1040"></a>goog.ui.ac.InputHandler.prototype.onTick_ = function(e) {
<a name="line1041"></a>  this.update();
<a name="line1042"></a>};
<a name="line1043"></a>
<a name="line1044"></a>
<a name="line1045"></a>/**
<a name="line1046"></a> * Handles typing in an inactive input element. Activate it.
<a name="line1047"></a> * @param {goog.events.BrowserEvent} e Browser event object.
<a name="line1048"></a> * @private
<a name="line1049"></a> */
<a name="line1050"></a>goog.ui.ac.InputHandler.prototype.onKeyDownOnInactiveElement_ = function(e) {
<a name="line1051"></a>  this.handleFocus(e);
<a name="line1052"></a>};
<a name="line1053"></a>
<a name="line1054"></a>
<a name="line1055"></a>/**
<a name="line1056"></a> * Handles typing in the active input element.  Checks if the key is a special
<a name="line1057"></a> * key and does the relevent action as appropriate.
<a name="line1058"></a> * @param {goog.events.BrowserEvent} e Browser event object.
<a name="line1059"></a> * @private
<a name="line1060"></a> */
<a name="line1061"></a>goog.ui.ac.InputHandler.prototype.onKey_ = function(e) {
<a name="line1062"></a>  this.lastKeyCode_ = e.keyCode;
<a name="line1063"></a>  if (this.ac_) {
<a name="line1064"></a>    this.handleKeyEvent(e);
<a name="line1065"></a>  }
<a name="line1066"></a>};
<a name="line1067"></a>
<a name="line1068"></a>
<a name="line1069"></a>/**
<a name="line1070"></a> * Handles a KEYPRESS event generated by typing in the active input element.
<a name="line1071"></a> * Checks if IME input is ended.
<a name="line1072"></a> * @param {goog.events.BrowserEvent} e Browser event object.
<a name="line1073"></a> * @private
<a name="line1074"></a> */
<a name="line1075"></a>goog.ui.ac.InputHandler.prototype.onKeyPress_ = function(e) {
<a name="line1076"></a>  if (this.waitingForIme_ &amp;&amp;
<a name="line1077"></a>      this.lastKeyCode_ != goog.events.KeyCodes.WIN_IME) {
<a name="line1078"></a>    this.stopWaitingForIme_();
<a name="line1079"></a>  }
<a name="line1080"></a>};
<a name="line1081"></a>
<a name="line1082"></a>
<a name="line1083"></a>/**
<a name="line1084"></a> * Handles the key-up event.  This is only ever used by Mac FF or when we are in
<a name="line1085"></a> * an IME entry scenario.
<a name="line1086"></a> * @param {goog.events.BrowserEvent} e Browser event object.
<a name="line1087"></a> * @private
<a name="line1088"></a> */
<a name="line1089"></a>goog.ui.ac.InputHandler.prototype.onKeyUp_ = function(e) {
<a name="line1090"></a>  if (this.waitingForIme_ &amp;&amp;
<a name="line1091"></a>      (e.keyCode == goog.events.KeyCodes.ENTER ||
<a name="line1092"></a>       (e.keyCode == goog.events.KeyCodes.M &amp;&amp; e.ctrlKey))) {
<a name="line1093"></a>    this.stopWaitingForIme_();
<a name="line1094"></a>  }
<a name="line1095"></a>};
<a name="line1096"></a>
<a name="line1097"></a>
<a name="line1098"></a>/**
<a name="line1099"></a> * Handles mouse-down event.
<a name="line1100"></a> * @param {goog.events.BrowserEvent} e Browser event object.
<a name="line1101"></a> * @private
<a name="line1102"></a> */
<a name="line1103"></a>goog.ui.ac.InputHandler.prototype.onMouseDown_ = function(e) {
<a name="line1104"></a>  if (this.ac_) {
<a name="line1105"></a>    this.handleMouseDown(e);
<a name="line1106"></a>  }
<a name="line1107"></a>};
<a name="line1108"></a>
<a name="line1109"></a>
<a name="line1110"></a>/**
<a name="line1111"></a> * For subclasses to override to handle the mouse-down event.
<a name="line1112"></a> * @param {goog.events.BrowserEvent} e Browser event object.
<a name="line1113"></a> * @protected
<a name="line1114"></a> */
<a name="line1115"></a>goog.ui.ac.InputHandler.prototype.handleMouseDown = function(e) {
<a name="line1116"></a>};
<a name="line1117"></a>
<a name="line1118"></a>
<a name="line1119"></a>/**
<a name="line1120"></a> * Starts waiting for IME.
<a name="line1121"></a> * @private
<a name="line1122"></a> */
<a name="line1123"></a>goog.ui.ac.InputHandler.prototype.startWaitingForIme_ = function() {
<a name="line1124"></a>  if (this.waitingForIme_) {
<a name="line1125"></a>    return;
<a name="line1126"></a>  }
<a name="line1127"></a>  this.eh_.listen(
<a name="line1128"></a>      this.activeElement_, goog.events.EventType.KEYUP, this.onKeyUp_);
<a name="line1129"></a>  this.eh_.listen(
<a name="line1130"></a>      this.activeElement_, goog.events.EventType.KEYPRESS, this.onKeyPress_);
<a name="line1131"></a>  this.waitingForIme_ = true;
<a name="line1132"></a>};
<a name="line1133"></a>
<a name="line1134"></a>
<a name="line1135"></a>/**
<a name="line1136"></a> * Stops waiting for IME.
<a name="line1137"></a> * @private
<a name="line1138"></a> */
<a name="line1139"></a>goog.ui.ac.InputHandler.prototype.stopWaitingForIme_ = function() {
<a name="line1140"></a>  if (!this.waitingForIme_) {
<a name="line1141"></a>    return;
<a name="line1142"></a>  }
<a name="line1143"></a>  this.waitingForIme_ = false;
<a name="line1144"></a>  this.eh_.unlisten(
<a name="line1145"></a>      this.activeElement_, goog.events.EventType.KEYPRESS, this.onKeyPress_);
<a name="line1146"></a>  this.eh_.unlisten(
<a name="line1147"></a>      this.activeElement_, goog.events.EventType.KEYUP, this.onKeyUp_);
<a name="line1148"></a>};
<a name="line1149"></a>
<a name="line1150"></a>
<a name="line1151"></a>/**
<a name="line1152"></a> * Handles the key-press event for IE, checking to see if the user typed a
<a name="line1153"></a> * separator character.
<a name="line1154"></a> * @param {goog.events.BrowserEvent} e Browser event object.
<a name="line1155"></a> * @private
<a name="line1156"></a> */
<a name="line1157"></a>goog.ui.ac.InputHandler.prototype.onIeKeyPress_ = function(e) {
<a name="line1158"></a>  this.handleSeparator_(e);
<a name="line1159"></a>};
<a name="line1160"></a>
<a name="line1161"></a>
<a name="line1162"></a>/**
<a name="line1163"></a> * Checks if an update has occurred and notified the autocomplete of the new
<a name="line1164"></a> * token.
<a name="line1165"></a> * @param {boolean=} opt_force If true the menu will be forced to update.
<a name="line1166"></a> */
<a name="line1167"></a>goog.ui.ac.InputHandler.prototype.update = function(opt_force) {
<a name="line1168"></a>  if (this.activeElement_ &amp;&amp;
<a name="line1169"></a>      (opt_force || this.getValue() != this.lastValue_)) {
<a name="line1170"></a>    if (opt_force || !this.rowJustSelected_) {
<a name="line1171"></a>      var token = this.parseToken();
<a name="line1172"></a>
<a name="line1173"></a>      if (this.ac_) {
<a name="line1174"></a>        this.ac_.setTarget(this.activeElement_);
<a name="line1175"></a>        this.ac_.setToken(token, this.getValue());
<a name="line1176"></a>      }
<a name="line1177"></a>    }
<a name="line1178"></a>    this.lastValue_ = this.getValue();
<a name="line1179"></a>  }
<a name="line1180"></a>  this.rowJustSelected_ = false;
<a name="line1181"></a>};
<a name="line1182"></a>
<a name="line1183"></a>
<a name="line1184"></a>/**
<a name="line1185"></a> * Parses a text area or input box for the currently highlighted token.
<a name="line1186"></a> * @return {string} Token to complete.
<a name="line1187"></a> * @protected
<a name="line1188"></a> */
<a name="line1189"></a>goog.ui.ac.InputHandler.prototype.parseToken = function() {
<a name="line1190"></a>  return this.parseToken_();
<a name="line1191"></a>};
<a name="line1192"></a>
<a name="line1193"></a>
<a name="line1194"></a>/**
<a name="line1195"></a> * Moves hilite up.  May hilite next or previous depending on orientation.
<a name="line1196"></a> * @return {boolean} True if successful.
<a name="line1197"></a> * @private
<a name="line1198"></a> */
<a name="line1199"></a>goog.ui.ac.InputHandler.prototype.moveUp_ = function() {
<a name="line1200"></a>  return this.upsideDown_ ? this.ac_.hiliteNext() : this.ac_.hilitePrev();
<a name="line1201"></a>};
<a name="line1202"></a>
<a name="line1203"></a>
<a name="line1204"></a>/**
<a name="line1205"></a> * Moves hilite down.  May hilite next or previous depending on orientation.
<a name="line1206"></a> * @return {boolean} True if successful.
<a name="line1207"></a> * @private
<a name="line1208"></a> */
<a name="line1209"></a>goog.ui.ac.InputHandler.prototype.moveDown_ = function() {
<a name="line1210"></a>  return this.upsideDown_ ? this.ac_.hilitePrev() : this.ac_.hiliteNext();
<a name="line1211"></a>};
<a name="line1212"></a>
<a name="line1213"></a>
<a name="line1214"></a>/**
<a name="line1215"></a> * Parses a text area or input box for the currently highlighted token.
<a name="line1216"></a> * @return {string} Token to complete.
<a name="line1217"></a> * @private
<a name="line1218"></a> */
<a name="line1219"></a>goog.ui.ac.InputHandler.prototype.parseToken_ = function() {
<a name="line1220"></a>  var caret = this.getCursorPosition();
<a name="line1221"></a>  var text = this.getValue();
<a name="line1222"></a>  return this.trim_(this.splitInput_(text)[this.getTokenIndex_(text, caret)]);
<a name="line1223"></a>};
<a name="line1224"></a>
<a name="line1225"></a>
<a name="line1226"></a>/**
<a name="line1227"></a> * Trims a token of characters that we want to ignore
<a name="line1228"></a> * @param {string} text string to trim.
<a name="line1229"></a> * @return {string} Trimmed string.
<a name="line1230"></a> * @private
<a name="line1231"></a> */
<a name="line1232"></a>goog.ui.ac.InputHandler.prototype.trim_ = function(text) {
<a name="line1233"></a>  return this.trimmer_ ? String(text).replace(this.trimmer_, &#39;&#39;) : text;
<a name="line1234"></a>};
<a name="line1235"></a>
<a name="line1236"></a>
<a name="line1237"></a>/**
<a name="line1238"></a> * Gets the index of the currently highlighted token
<a name="line1239"></a> * @param {string} text string to parse.
<a name="line1240"></a> * @param {number} caret Position of cursor in string.
<a name="line1241"></a> * @return {number} Index of token.
<a name="line1242"></a> * @private
<a name="line1243"></a> */
<a name="line1244"></a>goog.ui.ac.InputHandler.prototype.getTokenIndex_ = function(text, caret) {
<a name="line1245"></a>  // Split up the input string into multiple entries
<a name="line1246"></a>  var entries = this.splitInput_(text);
<a name="line1247"></a>
<a name="line1248"></a>  // Short-circuit to select the last entry
<a name="line1249"></a>  if (caret == text.length) return entries.length - 1;
<a name="line1250"></a>
<a name="line1251"></a>  // Calculate which of the entries the cursor is currently in
<a name="line1252"></a>  var current = 0;
<a name="line1253"></a>  for (var i = 0, pos = 0; i &lt; entries.length &amp;&amp; pos &lt;= caret; i++) {
<a name="line1254"></a>    pos += entries[i].length;
<a name="line1255"></a>    current = i;
<a name="line1256"></a>  }
<a name="line1257"></a>
<a name="line1258"></a>  // Get the token for the current item
<a name="line1259"></a>  return current;
<a name="line1260"></a>};
<a name="line1261"></a>
<a name="line1262"></a>
<a name="line1263"></a>/**
<a name="line1264"></a> * Splits an input string of text at the occurance of a character in
<a name="line1265"></a> * {@link goog.ui.ac.InputHandler.prototype.separators_} and creates
<a name="line1266"></a> * an array of tokens.  Each token may contain additional whitespace and
<a name="line1267"></a> * formatting marks.  If necessary use
<a name="line1268"></a> * {@link goog.ui.ac.InputHandler.prototype.trim_} to clean up the
<a name="line1269"></a> * entries.
<a name="line1270"></a> *
<a name="line1271"></a> * @param {string} text Input text.
<a name="line1272"></a> * @return {Array} Parsed array.
<a name="line1273"></a> * @private
<a name="line1274"></a> */
<a name="line1275"></a>goog.ui.ac.InputHandler.prototype.splitInput_ = function(text) {
<a name="line1276"></a>  if (!this.multi_) {
<a name="line1277"></a>    return [text];
<a name="line1278"></a>  }
<a name="line1279"></a>
<a name="line1280"></a>  var arr = String(text).split(&#39;&#39;);
<a name="line1281"></a>  var parts = [];
<a name="line1282"></a>  var cache = [];
<a name="line1283"></a>
<a name="line1284"></a>  for (var i = 0, inLiteral = false; i &lt; arr.length; i++) {
<a name="line1285"></a>    if (this.literals_ &amp;&amp; this.literals_.indexOf(arr[i]) != -1) {
<a name="line1286"></a>      if (this.generateNewTokenOnLiteral_ &amp;&amp; !inLiteral) {
<a name="line1287"></a>        parts.push(cache.join(&#39;&#39;));
<a name="line1288"></a>        cache.length = 0;
<a name="line1289"></a>      }
<a name="line1290"></a>      cache.push(arr[i]);
<a name="line1291"></a>      inLiteral = !inLiteral;
<a name="line1292"></a>
<a name="line1293"></a>    } else if (!inLiteral &amp;&amp; this.separators_.indexOf(arr[i]) != -1) {
<a name="line1294"></a>      cache.push(arr[i]);
<a name="line1295"></a>      parts.push(cache.join(&#39;&#39;));
<a name="line1296"></a>      cache.length = 0;
<a name="line1297"></a>
<a name="line1298"></a>    } else {
<a name="line1299"></a>      cache.push(arr[i]);
<a name="line1300"></a>    }
<a name="line1301"></a>  }
<a name="line1302"></a>  parts.push(cache.join(&#39;&#39;));
<a name="line1303"></a>
<a name="line1304"></a>  return parts;
<a name="line1305"></a>};
</pre>


</body>
</html>
